# coding=utf-8
import boto3
from emr.emr_cluster_provision import EmrClusterProvision


class EmrCluster(object):
    def __init__(self, cluster_name):
        self.client = boto3.client('emr')
        self.cluster_name = cluster_name

    def submit_job(self, steps_name, input_parameters=None):
        module_name = steps_name
        lib = __import__(module_name)
        if not lib.__file__:
            raise ImportError("please add built-in module into sys path.  e.g. sys.path.append('xxx')...")

        steps = lib.generate_steps(input_parameters)
        master_type = lib.master_type if hasattr(lib, "master_type") else None
        instance_type = lib.instance_type if hasattr(lib, "instance_type") else None
        instance_count = lib.instance_count if hasattr(lib, "instance_count") else None
        driver_memory = lib.driver_memory if hasattr(lib, "driver_memory") else None
        executor_memory = lib.executor_memory if hasattr(lib, "executor_memory") else None
        bootstrap_env = lib.bootstrap_env if hasattr(lib, "bootstrap_env") else None
        python_version = lib.python_version if hasattr(lib, "python_version") else None

        provision = EmrClusterProvision(
            master_type, instance_type, instance_count,
            driver_memory, executor_memory, bootstrap_env,
            python_version)

        provision.append_steps(steps)

        response = self.client.run_job_flow(
            Name=self.cluster_name,
            LogUri='s3://pt-emr-common/Log',
            ReleaseLabel=provision.releaseLabel,
            Instances=provision.instances,
            Steps=provision.steps,
            BootstrapActions=provision.bootstrapActions,
            Applications=provision.applications,
            Configurations=provision.configurations,
            VisibleToAllUsers=provision.visibleToAllUsers,
            JobFlowRole=provision.jobFlowRole,
            ServiceRole=provision.serviceRole
        )
        return response

